<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title></title>
    {include file='header'}
</head>

<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a>首页</a>
            <a href="{:url('index')}">广告管理</a>
            <a><cite>编辑</cite></a>
        </span>
        <div style="float: right;">
            <a class="layui-btn layui-btn-small" href="javascript:location.replace(location.href);" title="刷新">
                <i class="layui-icon layui-icon-refresh-1"></i></a>
        </div>
    </div>
    <div class="x-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label label-required">标题</label>
                <div class="layui-input-block">
                    <input type="text" id="title" name="title" value="{if !empty($data)}{$data.title}{/if}"
                        lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类</label>
                <div class="layui-input-block">
                    <select name="category_id" lay-filter="category_id">
                        <option value="0">所属分类</option>
                        {foreach $category as $v}
                        <option value="{$v.id}" {if !empty($data)}{if $v.id==$data.category_id} selected{/if}{/if}>
                            {$v.name}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red"></span>跳转</label>
                <div class="layui-input-block">
                    <input type="text" id="url" name="url" value="{if !empty($data)}{$data.url}{/if}" autocomplete="off"
                        class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-label label-required">图片</label>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" id="images">
                            <i class="layui-icon">&#xe67c;</i>上传
                        </button>
                        <div class="demo-images">{if !empty($data.images)}
                            {foreach $data.images as $v}
                            <div class="upload-list"><img data-url="{$v}" src="{$v}" class="upload-img"
                                    ondblclick="removeimg(this)" /><span class="upload-remove"
                                    onclick="remove_img(this)">✖</span></div>
                            {/foreach}
                            {/if}
                        </div>
                        <input name="images" type="hidden" value="{$data.images_str|default=''}">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red"></span>内容</label>
                <div class="layui-input-block">
                    <textarea name="contents" id="contents"
                        style="width:100%;height:100px;display: block;">{if !empty($data)}{$data.contents}{/if}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" id="sort" name="sort" value="{if !empty($data)}{$data.sort}{else}0{/if}"
                        autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" value="1" lay-text="开启|停用" {if !empty($data)}{if
                        $data.status==1} checked="" {/if}{/if} lay-skin="switch">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <input type="hidden" id="id" name="id" value="{if !empty($data)}{$data.id}{/if}" />
                <button type="button" class="layui-btn btn-fix" lay-filter="save" lay-submit="">保存</button>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        layui.use('upload', function () {
            var upload = layui.upload;
            var uploadInst = upload.render({
                elem: '#images'
                , url: "{:url('zongtai/upload/index')}"
                , data: { type: "banner" }//额外传输的参数
                // ,exts: 'jpg|png|gif|' //可传输文件的后缀
                // ,size: 100 //传输大小100k
                // ,headers:{token:'XXXXXXXX'} //额外添加的请求头
                // ,multiple: false //多文件上传
                , done: function (res) {
                    console.log(res)
                    if (res.code == 1) {
                        var imgname = $(".demo-images").html();
                        var filepath = res.data.url;
                        var imgo = '<div class="upload-list"><img data-url="' + filepath + '" src="' + filepath + '" class="upload-img" ondblclick="removeimg(this)"/><span class="upload-remove" onclick="remove_img(this)">✖</span></div>'
                        imgname = imgname + imgo;
                        $(".demo-images").html(imgname);
                        regetimg();
                    }
                    else {
                        layer.msg(res.msg)
                    }
                }
                , error: function () {
                    layer.msg('上传失败');
                }
            });
        });
        layui.use(['upload', 'form'], function () {
            var form = layui.form;
            //监听提交
            form.on('submit(save)', function (data) {
                console.log(data.field)
                $.ajax({
                    type: "POST",
                    url: "{:url('save')}",
                    data: data.field,
                    datatype: "json",
                    success: function (res, textStatus) {
                        console.log(res)
                        if (res.code == 0) {
                            layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                window.parent.location.reload();
                                parent.layer.close(index);
                            });
                        }
                        else {
                            layer.msg(res.msg, { icon: 5 });//0感叹号  1打勾  2打叉  3问号  4锁头  5难过  6笑脸
                            return false;
                        }
                    },
                    error: function (e) {
                        layer.alert('系统出现错误,请检查系统！', { icon: 2 });
                        return false;
                    }
                });
                return false;
            });
        });
    </script>
    {include file='footer'}